
/*--------------------------------------------------------------------------
  
  This plugin adds a command to the config screen to return to the home screen (main menu).
  
  Author:
  SapphireSoft
  https://srpgstudio.com/
  
  History:
  2024/07/25 Released
  
--------------------------------------------------------------------------*/

(function() {

StringTable.GoHome = 'To Title Screen';
StringTable.GoHomeDesc = 'Go back to the title screen?';
StringTable.GoHomeOption = 'To Title Screen';
StringTable.GoHomeOptionDesc = 'Decide whether to return to the title screen.';

var HomeCommand = defineObject(BaseListCommand,
{
	_questionWindow: null,
	
	openCommand: function() {
		this._questionWindow = createWindowObject(QuestionWindow, this);
		this._questionWindow.setQuestionMessage(StringTable.GoHomeDesc);
		this._questionWindow.setQuestionActive(true);
	},
	
	moveCommand: function() {
		if (this._questionWindow.moveWindow() !== MoveResult.CONTINUE) {
			if (this._questionWindow.getQuestionAnswer() === QuestionAnswer.YES) {
				this._doEndAction();
			}
			return MoveResult.END;
		}
		
		return MoveResult.CONTINUE;
	},
	
	drawCommand: function() {
		var x = LayoutControl.getCenterX(-1, this._questionWindow.getWindowWidth());
		var y = LayoutControl.getCenterY(-1, this._questionWindow.getWindowHeight());
		
		this._questionWindow.drawWindow(x, y);
	},
	
	getCommandName: function() {
		return StringTable.GoHome;
	},
	
	isCommandDisplayable: function() {
		return root.getExternalData().env.goHome !== 1;
	},
	
	_doEndAction: function() {
		root.resetGame();
	}
}
);

var alias1 = SetupCommand.configureCommands;
SetupCommand.configureCommands = function(groupArray) {
	alias1.call(this, groupArray);
	
	groupArray.insertObject(SetupCommand.Home, groupArray.length - 1);
};

SetupCommand.Home = defineObject(HomeCommand,
{
}
);

var alias2 = MapCommand.configureCommands;
MapCommand.configureCommands = function(groupArray) {
	alias2.call(this, groupArray);
	
	groupArray.insertObject(MapCommand.Home, groupArray.length - 1);
};

MapCommand.Home = defineObject(HomeCommand,
{
}
);

var alias3 = RestCommand.configureCommands;
RestCommand.configureCommands = function(groupArray) {
	alias3.call(this, groupArray);
	
	groupArray.insertObject(RestCommand.Home, groupArray.length - 1);
};

RestCommand.Home = defineObject(HomeCommand,
{
}
);

var alias10 = ConfigWindow._configureConfigItem;
ConfigWindow._configureConfigItem = function(groupArray) {
	alias10.call(this, groupArray);
	
	groupArray.insertObject(ConfigItem.Home, groupArray.length);
};

ConfigItem.Home = defineObject(BaseConfigtItem,
{
	selectFlag: function(index) {
		root.getExternalData().env.goHome = index;
	},
	
	getFlagValue: function() {
		if (typeof root.getExternalData().env.goHome !== 'number') {
			return 0;
		}
	
		return root.getExternalData().env.goHome;
	},
	
	getConfigItemTitle: function() {
		return StringTable.GoHomeOption;
	},
	
	getConfigItemDescription: function() {
		return StringTable.GoHomeOptionDesc;
	}
}
);

})();
